#include "resourcewindow.h"

#include "qresourcetoplevelwidget.h"
#include "qresourcefilewidget.h"
#include "imageview.h"

#include "../../lib/corelib/styledbar.h"
#include "../../lib/corelib/minisplitter.h"
#include "../../include/abstractcore.h"
#include "../../include/resourceinterface.h"
#include "../../lib/corelib/qactiontoolbar.h"

#include <QVBoxLayout>
#include <QMessageBox>
#include <QFileDialog>



QAddImageFolderDialog::QAddImageFolderDialog(QResourceInterface *interface, QWidget *parent):
    QDialog(parent),
    m_resource(interface),
    m_ret(0)
{
    setWindowTitle(tr("Add New Image Folder"));
    this->resize(350,100);
    QPushButton *btn;

    btn=new QPushButton(tr("Add"),this);
    btn->resize(75,23);
    btn->move(80,60);
    connect(btn,SIGNAL(clicked()),this,SLOT(onAdd()));

    btn=new QPushButton(tr("Cancel"),this);
    btn->resize(73,23);
    btn->move(210,60);
    connect(btn,SIGNAL(clicked()),this,SLOT(onCancel()));

    QLabel *l=new QLabel(tr("Name:"),this);
    l->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
    l->resize(110,30);
    l->move(0,20);

    m_edit=new QLineEdit(this);
    m_edit->resize(190,20);
    m_edit->move(120,25);

    m_edit->setFocus();
    setMaximumSize(350,100);
    setMinimumSize(350,100);

}

void QAddImageFolderDialog::onAdd()
{
    QString str=m_edit->text();

    if(str=="")
    {
        QMessageBox::warning(this,tr("Warning"),tr("Image folder name can not be empty!"));
        return;
    }

    QStringList list=m_resource->getTopLevelNames();

    if(list.indexOf(str)>=0)
    {
        QMessageBox::warning(this,tr("Warning"),tr("This name has been used!"));
        return;
    }

    m_ret=1;
    close();
}

void QAddImageFolderDialog::onCancel()
{
    close();
}

QString QAddImageFolderDialog::getFolder()
{
    return m_edit->text();
}

int QAddImageFolderDialog::getRet()
{
    return m_ret;
}

ResourceWindow::ResourceWindow(AbstractCore *core, QWidget *parent):
    QMainWindow(parent),
    m_core(core),
    m_splitter(new MiniSplitter(this))
{
    m_toplevel=new QResourceTopLevelWidget(core,this);
    m_fileWidget=new QResourceFileWidget(core,this);
    m_imageView=new ImageView(this);

    QWidget* wid;

    QVBoxLayout *l;

    MiniSplitter *splitter=new MiniSplitter(Qt::Vertical);

    wid=new QWidget(this);

    connect(m_toplevel,SIGNAL(selectChange()),this,SLOT(toplevelSelectChange()));
    connect(m_fileWidget,SIGNAL(selectChange()),this,SLOT(resourceSelectChanged()));
    m_toplevelBar=new StyledBar(this);
    l=new QVBoxLayout(wid);
    l->setMargin(0);
    l->setSpacing(0);
    l->addWidget(m_toplevelBar);
    l->addWidget(m_toplevel);
    QResourceInterface *res=core->resourceInterface();
    QStringList list=res->getTopLevelNames();

    for(int i=0;i<list.size();i++)
    {
        m_toplevel->addTopFile(list.at(i));
    }

    if(list.size()>0)
    {
        m_toplevel->select(list.first());
    }
    else
    {
        m_toplevel->select("");
    }

    m_topLevelToolBar=new QActionToolBar(this);

    QVBoxLayout *vl=new QVBoxLayout(m_toplevelBar);
    vl->setMargin(1);
    vl->setSpacing(0);
    vl->addWidget(m_topLevelToolBar);

    QAction *ac;
    QList<QAction*> actionList;

    ac=new QAction(this);
    ac->setIcon(QIcon(":/images/append.png"));
    ac->setText(tr("Add New Image Folder"));
    connect(ac,SIGNAL(triggered()),this,SLOT(addImageFolder()));
    actionList.append(ac);
    m_actions.insert("add_folder",ac);

    ac=new QAction(this);
    ac->setIcon(QIcon(":/images/remove.png"));
    ac->setText(tr("Remove Image Folder"));
    connect(ac,SIGNAL(triggered()),this,SLOT(removeFolder()));
    actionList.append(ac);
    m_actions.insert("remove_folder",ac);

    ac=new QAction(this);
    ac->setSeparator(true);
    actionList.append(ac);

    m_topLevelToolBar->addButtonActions(actionList);

    splitter->addWidget(wid);


    wid=new QWidget(this);
    m_fileWidgetBar=new StyledBar(this);
    l=new QVBoxLayout(wid);
    l->setMargin(0);
    l->setSpacing(0);
    l->addWidget(m_fileWidgetBar);
    l->addWidget(m_fileWidget);

    m_fileWidgetToolBar=new QActionToolBar(this);
    vl=new QVBoxLayout(m_fileWidgetBar);
    vl->setMargin(1);
    vl->setSpacing(0);
    vl->addWidget(m_fileWidgetToolBar);

    actionList.clear();

    ac=new QAction(this);
    ac->setIcon(QIcon(":/images/append.png"));
    ac->setText(tr("Add New Image"));
    connect(ac,SIGNAL(triggered()),this,SLOT(addImage()));
    actionList.append(ac);
    m_actions.insert("add_resource",ac);

    ac=new QAction(this);
    ac->setIcon(QIcon(":/images/remove.png"));
    ac->setText(tr("Remove Image"));
    connect(ac,SIGNAL(triggered()),this,SLOT(removeImage()));
    actionList.append(ac);
    m_actions.insert("remove_resource",ac);

    ac=new QAction(this);
    ac->setSeparator(true);
    actionList.append(ac);

    m_fileWidgetToolBar->addButtonActions(actionList);

    splitter->addWidget(wid);

    splitter->resize(300,300);

    m_splitter->addWidget(splitter);

    splitter=new MiniSplitter(Qt::Vertical);

    wid=new QWidget(this);
    m_imageViewBar=new StyledBar(this);
    l=new QVBoxLayout(wid);
    l->setMargin(0);
    l->setSpacing(0);
    l->addWidget(m_imageViewBar);
    l->addWidget(m_imageView);

    m_imageViewToolBar=new QActionToolBar(this);
    vl=new QVBoxLayout(m_imageViewBar);
    vl->setMargin(1);
    vl->setSpacing(0);
    vl->addWidget(m_imageViewToolBar);
    actionList.clear();

    ac=new QAction(this);
    ac->setIcon(QIcon(":/images/fitinscreen.png"));
    ac->setText(tr("Fit image in the screen"));
    connect(ac,SIGNAL(triggered()),m_imageView,SLOT(fitToScreen()));
    actionList.append(ac);
    m_actions.insert("ImageView_Fitscreen",ac);

    ac=new QAction(this);
    ac->setIcon(QIcon(":/images/originalsize.png"));
    ac->setText(tr("Original size"));
    connect(ac,SIGNAL(triggered()),m_imageView,SLOT(resetToOriginalSize()));
    actionList.append(ac);
    m_actions.insert("ImageView_OriginalSize",ac);

    ac=new QAction(this);
    ac->setIcon(QIcon(":/images/zoomin.png"));
    ac->setText(tr("Zoom In"));
    connect(ac,SIGNAL(triggered()),m_imageView,SLOT(zoomIn()));
    actionList.append(ac);
    m_actions.insert("ImageView_ZoomIn",ac);

    ac=new QAction(this);
    ac->setIcon(QIcon(":/images/zoomout.png"));
    ac->setText(tr("Zoom Out"));
    connect(ac,SIGNAL(triggered()),m_imageView,SLOT(zoomOut()));
    actionList.append(ac);
    m_actions.insert("ImageView_ZoomOut",ac);

    ac=new QAction(this);
    ac->setSeparator(true);
    actionList.append(ac);

    m_imageViewToolBar->addButtonActions(actionList);

    m_splitter->addWidget(wid);

    m_splitter->setStretchFactor(0,0);
    m_splitter->setStretchFactor(1,1);

    m_splitter->setVisible(true);


    setCentralWidget(m_splitter);
}

void ResourceWindow::addImageFolder()
{
    QAddImageFolderDialog dlg(m_core->resourceInterface(),this);
    dlg.exec();

    if(dlg.getRet()==1)
    {
        m_core->resourceInterface()->addTopLevel(dlg.getFolder());
        m_toplevel->addTopFile(dlg.getFolder());
        m_toplevel->select(dlg.getFolder());
        m_core->resourceInterface()->save(dlg.getFolder());
    }
}

void ResourceWindow::removeFolder()
{
    QString str=m_toplevel->getSelect();
    if(m_core->resourceInterface()->topLevelCandel(str))
    {
        m_core->resourceInterface()->removeTopLevel(str);
        m_toplevel->removeTopFile(str);
        if(m_core->resourceInterface()->getTopLevelNames().size()>0)
        {
            m_toplevel->select(m_core->resourceInterface()->getTopLevelNames().first());
        }
        else
        {
            m_toplevel->select("");
        }
        m_core->resourceInterface()->save(str);
    }
}

void ResourceWindow::toplevelSelectChange()
{
    m_fileWidget->clear();

    QString str=m_toplevel->getSelect();
    if(str!="")
    {
        QStringList list=m_core->resourceInterface()->getResourceNames(str);

        for(int i=0;i<list.size();i++)
        {
            m_fileWidget->addResourceFile(str+"/"+list.at(i));
        }
        if(list.size()>0)
        {
            m_fileWidget->select(list.at(0));
        }
        else
        {
            resourceSelectChanged();
        }
    }
    else
    {
        m_fileWidget->select("");
    }
}

void ResourceWindow::addImage()
{

    QString toplevel=m_toplevel->getSelect();
    if(toplevel=="")
    {
        return;
    }
    QStringList names=QFileDialog::getOpenFileNames(this,tr("Add Resource"),QDir::currentPath(),tr("Images (*.png *.xpm *.jpg)"));

    int index;
    QString name;
    QStringList list;
    for(int i=0;i<names.size();i++)
    {
        name=names.at(i);
        name.replace("\\","/");
        index=name.lastIndexOf("/");
        if(index>=0)
        {
            name=name.mid(index+1);
        }
        if(name!="")
        {
            QFile f(names.at(i));
            if(f.open(QIODevice::ReadOnly))
            {
                m_core->resourceInterface()->addResource(toplevel,name,f.readAll());
                m_fileWidget->addResourceFile(toplevel+"/"+name);
                list.append(name);
                f.close();
            }
        }
    }
    if(list.size()>0)
    {
        m_fileWidget->select(list.first());
        m_core->resourceInterface()->save(toplevel);
    }
}

void ResourceWindow::removeImage()
{
    QString str=m_toplevel->getSelect();
    QString res=m_fileWidget->getSelect();
    if(m_core->resourceInterface()->resourceCandel(str,res))
    {
        m_core->resourceInterface()->removeResource(str,res);
        m_fileWidget->removeResourceFile(res);
        if(m_core->resourceInterface()->getResourceNames(str).size())
        {
            m_fileWidget->select(m_core->resourceInterface()->getResourceNames(str).first());
        }
        else
        {
            m_fileWidget->select("");
        }
        m_core->resourceInterface()->save(str);
    }
}

void ResourceWindow::resourceSelectChanged()
{
    QString toplevel=m_toplevel->getSelect();
    QString resource=m_fileWidget->getSelect();

    QByteArray b=m_core->resourceInterface()->getResourceData(toplevel,resource);
    m_imageView->setImage(b);
}
